let tableIns;
let tree;
layui.use(['element', 'form', 'table', 'layer', 'laydate', 'tree', 'util'], function () {
    let table = layui.table;
    let form = layui.form;//select、单选、复选等依赖form
    let element = layui.element; //导航的hover效果、二级菜单等功能，需要依赖element模块
    let laydate = layui.laydate;
    tree = layui.tree;
    let height = document.documentElement.clientHeight - 60;

    tableIns = table.render({
        elem: '#accountTable'
        , url: ctx + '/agent/account/accountlist/page'
        , method: 'POST'
        //请求前参数处理
        , request: {
            pageName: 'page' //页码的参数名称，默认：page
            , limitName: 'rows' //每页数据量的参数名，默认：limit
        }
        , response: {
            statusName: 'flag' //规定数据状态的字段名称，默认：code
            , statusCode: true //规定成功的状态码，默认：0
            , msgName: 'msg' //规定状态信息的字段名称，默认：msg
            , countName: 'records' //规定数据总数的字段名称，默认：count
            , dataName: 'rows' //规定数据列表的字段名称，默认：data
        }
        //响应后数据处理
        , parseData: function (res) { //res 即为原始返回的数据
            var data = res.data;
            return {
                "flag": res.flag, //解析接口状态
                "msg": res.msg, //解析提示文本
                "records": data.records, //解析数据长度
                "rows": data.rows //解析数据列表
            };
        }
        , toolbar: '#accountTableToolbar'
        , title: '代理商列表'
        , cols: [[
        	{field: 'zizeng', title: '序号', width:80, align:'center', 
        		templet: function(d){return d.LAY_INDEX;}}
        	
            , {field: 'agentId', title: '代理商ID', hide: true}
            , {field: 'agentName',title: '代理商'}
            , {field: 'agentPerson', title: '联系人', hide: true}
            , {field: 'agentPhone',width:150, title: '联系电话', hide: true}
            
            , {field: 'agentAccountId',title: '虚拟账号ID', hide: true}
            , {field: 'agentAccount',title: '虚拟账号', hide: true}
            , {field: 'agentAccountBalance',title: '余额汇总', align:'right', style:'font-weight:bold',
        		templet: function(d){return formatMoney(d.agentAccountBalance);}}
            
            , {field: 'agentAccountIncome',title: '收入汇总', align:'right', style:'font-weight:bold',
        		templet: function(d){return formatMoney(d.agentAccountIncome);}}
            
            , {field: 'agentAccountPay',title: '支出汇总', align:'right', style:'font-weight:bold',
        		templet: function(d){return formatMoney(d.agentAccountPay);}}
            
            , {field: 'agentAccountCommission',title: '返佣汇总', align:'right', style:'font-weight:bold',
        		templet: function(d){return formatMoney(d.agentAccountCommission);}}
            
            , {field: 'agentAccountFreeze',title: '冻结余额', align:'right', style:'font-weight:bold',
        		templet: function(d){return formatMoney(d.agentAccountFreeze);}}
            , {field: 'cpmyue',title: '月结分润', align:'right', style:'font-weight:bold',
        		templet: function(d){return formatMoney(d.cpmyue);}}
            , {field: 'cpmji',title: '季结分润' }
            
            
            , {fixed: 'right', width:200, title: '操作', toolbar: '#accountTableBar'}
        ]]
        , defaultToolbar: ['', '', '']
        , page: true
        , height: height
        , cellMinWidth: 80
    });

    //头工具栏事件
    table.on('toolbar(display)', function (obj) {
        switch (obj.event) {
            case 'addData':
            	layer.open({
                    type: 2,
                    title: '新增代理商',
                    closeBtn: 1, //显示关闭按钮
                    anim: 2,
                    area: ['800px', '450px'],
                    shadeClose: true, //开启遮罩关闭
                    content: '/agent/agentinsert'
                });
                break;
            case 'query':
                let queryByAgentName = $("#queryByAgentName").val();
                let query = {
                    page: {
                        curr: 1 //重新从第 1 页开始
                    }
                    , done: function (res, curr, count) {
                        //完成后重置where，解决下一次请求携带旧数据
                        this.where = {};
                    }
                };
                if (queryByAgentName) {
                    //设定异步数据接口的额外参数
                    query.where = {agentName: queryByAgentName};
                }
                tableIns.reload(query);
                $("#queryByAgentName").val(queryByAgentName);
                break;
        }
    });

    //监听行工具事件
    table.on('tool(display)', function (obj) {
        let data = obj.data;
        //扣款
        if (obj.event === 'delmoney') {
        	layer.open({
                type: 2,
                title: '代理商扣款',
                closeBtn: 1, //显示关闭按钮
                anim: 2,
                area: ['600px', '400px'],
                shadeClose: true, //开启遮罩关闭
                content: '/agent/account/delmoney/'+data.agentAccountId,
                success: function(layero, index){
                	
                }
            });
        }

        // 加款
        else if (obj.event === 'addmoney') {
        	layer.open({
                type: 2,
                title: '代理商加款',
                closeBtn: 1, //显示关闭按钮
                anim: 2,
                area: ['600px', '400px'],
                shadeClose: true, //开启遮罩关闭
                content: '/agent/account/addmoney/'+data.agentAccountId,
                success: function(layero, index){
                	
                }
            });
        }
        
        // 交易流水
        else if (obj.event === 'showmoney') {
        	layer.open({
                type: 2,
                title: '代理商交易流水',
                closeBtn: 1, //显示关闭按钮
                anim: 2,
                area: ['1000px', '450px'],
                shadeClose: true, //开启遮罩关闭
                content: '/agent/account/detaillist/'+data.agentAccountId,
                success: function(layero, index){
                	
                }
            });
        }
    });
});

/**
 * 提交保存
 */
function accountFormSave() {
    let accountForm = $("#accountForm").serializeObject();
    var reg = /(^[1-9](\d+)?(\.\d{1,2})?$)|(^0$)|(^\d\.\d{1,2}$)/

    if(!reg.test(accountForm.operationMoney) && null != accountForm.operationMoney){
        layer.msg('金额输入不正确',{icon: 5});
        return false;
    }
    
    if(accountForm.tradeComment == null || accountForm.tradeComment == ''){
    	 layer.msg('备注为必填项',{icon: 5});
         return false;
    }
    
    
    save();
}

function save(){
	let accountForm = $("#accountForm").serializeObject();
    $.post(ctx + "/agent/account/operation", accountForm, function (data) {
    	if(data.flag){
	        layer.msg("代理商调额成功", {icon: 1, time: 2000}, function(){
	        	var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
	        	parent.layer.close(index); //再执行关闭
	        	window.parent.$("#queryByAgentNameBtn").click();
	        });
    	} else {
    		layer.msg("代理商调额失败!", {icon: 5}); 
    	}
    });
}